{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "login success!\n",
      "login respond error_code:0\n",
      "login respond  error_msg:success\n",
      "请输入股票代码，例如茅台为:sh.600519，平安银行为sz.000001等:sz.000858\n",
      "         证券号        财报日期     财报统计截止日 净资产收益率(平均)     销售净利率     销售毛利率  \\\n",
      "0  sz.000858  2020-04-28  2020-03-31   0.098592  0.398281  0.765316   \n",
      "1  sz.000858  2020-08-28  2020-06-30   0.143878  0.369577  0.745393   \n",
      "2  sz.000858  2020-10-30  2020-09-30   0.188184  0.358341  0.745307   \n",
      "3  sz.000858  2021-04-28  2020-12-31   0.249440  0.364846  0.741597   \n",
      "4  sz.000858  2021-04-28  2021-03-31   0.103174  0.402183  0.764319   \n",
      "\n",
      "               净利润(元)      每股收益           主营营业收入(元)            总股本  \\\n",
      "0   8060492743.980000  4.799903                      3881608005.00   \n",
      "1  11370985057.640000  4.874763  30767525988.400000  3881608005.00   \n",
      "2  15226890133.110000  4.998978                      3881608005.00   \n",
      "3  20913340409.540000  5.140862  57321059453.150000  3881608005.00   \n",
      "4   9783044789.880000  5.558040                      3881608005.00   \n",
      "\n",
      "            流通股本    净资产收益率      权益乘数    总资产周转率 归属母公司股东的净利润      净利润率  \\\n",
      "0  3795757968.00  0.098592  1.355521  0.191063    0.955809  0.398281   \n",
      "1  3795811143.00  0.143878  1.330613  0.306471    0.954657  0.369577   \n",
      "2  3795811143.00  0.188184  1.335011  0.411800    0.955248  0.358341   \n",
      "3  3795786270.00  0.249440  1.376842  0.520414    0.954167  0.364846   \n",
      "4  3795786270.00  0.103174  1.295350  0.207802    0.953038  0.402183   \n",
      "\n",
      "  税负水平(净利润/利润总额) 利息负担(利润总额/息税前利润)     经营利润率  \n",
      "0       0.753245         1.038842  0.508983  \n",
      "1       0.758011         1.052042  0.463444  \n",
      "2       0.756616         1.059873  0.446855  \n",
      "3       0.755582         1.058186  0.456316  \n",
      "4       0.752134         1.036160  0.516063  \n",
      "logout success!\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<baostock.data.resultset.ResultData at 0x2061ada51f0>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##查询某只股票的财务状况能力\n",
    "import baostock as bs\n",
    "import pandas as pd\n",
    "\n",
    "# 登陆系统\n",
    "lg = bs.login()\n",
    "# 显示登陆返回信息\n",
    "print('login respond error_code:'+lg.error_code)\n",
    "print('login respond  error_msg:'+lg.error_msg)\n",
    "stocknum = input(\"请输入股票代码，例如茅台为:sh.600519，平安银行为sz.000001等:\")\n",
    "\n",
    "# 查询季频估值指标盈利能力\n",
    "profit_list = []\n",
    "rs1= bs.query_profit_data(code=stocknum, year=2020, quarter=1)##获取2020年第一季盈利能力\n",
    "rs2= bs.query_profit_data(code=stocknum, year=2020, quarter=2)##获取2020年第二季盈利能力\n",
    "rs3= bs.query_profit_data(code=stocknum, year=2020, quarter=3)##获取2020年第三季盈利能力\n",
    "rs4= bs.query_profit_data(code=stocknum, year=2020, quarter=4)\n",
    "rs5= bs.query_profit_data(code=stocknum, year=2021, quarter=1)\n",
    "\n",
    "while (rs1.error_code == '0') & rs1.next():\n",
    "    profit_list.append(rs1.get_row_data())\n",
    "    profit_list.append(rs2.get_row_data())\n",
    "    profit_list.append(rs3.get_row_data())\n",
    "    profit_list.append(rs4.get_row_data())\n",
    "    profit_list.append(rs5.get_row_data())\n",
    "resp = pd.DataFrame(profit_list,columns=rs1.fields)##导入Pandas\n",
    "resp.rename(columns={'code':'证券号'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'pubDate':'财报日期'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'statDate':'财报统计截止日'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'roeAvg':'净资产收益率(平均)'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'npMargin':'销售净利率'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'gpMargin':'销售毛利率'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'netProfit':'净利润(元)'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'MBRevenue':'主营营业收入(元)'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'totalShare':'总股本'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'liqaShare':'流通股本'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp.rename(columns={'epsTTM':'每股收益'},inplace=True) #将表头的名称改成统一的名称\n",
    "\n",
    "#获取公司业绩快报 #\n",
    "result_list = []\n",
    "rs6 = bs.query_dupont_data(code=stocknum, year=2020, quarter=1)\n",
    "rs7 = bs.query_dupont_data(code=stocknum, year=2020, quarter=2)\n",
    "rs8 = bs.query_dupont_data(code=stocknum, year=2020, quarter=3)\n",
    "rs9 = bs.query_dupont_data(code=stocknum, year=2020, quarter=4)\n",
    "rs10 = bs.query_dupont_data(code=stocknum, year=2021, quarter=1)\n",
    "\n",
    "while (rs6.error_code == '0') & rs6.next():\n",
    "    result_list.append(rs6.get_row_data())\n",
    "    result_list.append(rs7.get_row_data())\n",
    "    result_list.append(rs8.get_row_data())\n",
    "    result_list.append(rs9.get_row_data())\n",
    "    result_list.append(rs10.get_row_data())\n",
    "    # 获取一条记录，将记录合并在一起\n",
    "resp2 = pd.DataFrame(result_list, columns=rs6.fields)\n",
    "resp2.rename(columns={'code':'证券号'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'pubDate':'财报日期'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'statDate':'财报统计截止日'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontROE':'净资产收益率'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontAssetStoEquity':'权益乘数'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontAssetTurn':'总资产周转率'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontPnitoni':'归属母公司股东的净利润'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontNitogr':'净利润率'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontTaxBurden':'税负水平(净利润/利润总额)'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontIntburden':'利息负担(利润总额/息税前利润)'},inplace=True) #将表头的名称改成统一的名称\n",
    "resp2.rename(columns={'dupontEbittogr':'经营利润率'},inplace=True) #将表头的名称改成统一的名称\n",
    "\n",
    "respall=pd.merge(resp,resp2)\n",
    "# 打印输出\n",
    "print(respall)\n",
    "# 结果集输出到csv文件\n",
    "#result_profit.to_csv(\"D:\\\\profit_data.csv\", encoding=\"gbk\", index=False)\n",
    "# 登出系统\n",
    "bs.logout()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
